package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.SysUserMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.SysUser;
import com.wanghaixin.service.SysUserService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Auther: 王海新
 * @Date: 2020/10/21 20:50
 * @Description:
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public List<SysUser> findAll() {
        return sysUserMapper.selectByExample(null);
    }

    @Override
    public DataGridResult findUserByPage(QueryDTO queryDTO) {
        //分页插件 拿到分页数据
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());

        //判断是否设置id排序
        if (queryDTO.getSort() != null && !queryDTO.getSort().equals("")) {
            queryDTO.setSort("user_id");
        }
        //将取回的数据放到DataGridResult
        List<SysUser> sysUser = sysUserMapper.findByPage(queryDTO);
        PageInfo pageInfo = new PageInfo<SysUser>(sysUser);
        Long data = pageInfo.getTotal();
        DataGridResult dataGridResult = new DataGridResult(data,pageInfo.getList());

        return dataGridResult;
    }

    @Override
    public Workbook exportUser() {
        //1.创建一个空的excel文件
        Workbook workbook = new HSSFWorkbook();
        //2.填充数据：创建sheet
        Sheet sheet = workbook.createSheet("某某公司的员工信息");
        //3.标题数组
        String titles[] = {"用户id","用户名","邮箱","电话","创建时间"};
        String colums[] = {"userId","username","email","mobile","createTime"};
        List<Map<String,Object>> maps = sysUserMapper.exportUser();
        Row rowTile = sheet.createRow(0);
        //标题行
        for (int i = 0; i <titles.length ; i++) {
            Cell cell = rowTile.createCell(i);
            cell.setCellValue(titles[i]);
        }
        //遍历数据填充到单元格
        for (int i = 0; i < maps.size(); i++) {
            //一条数据应该创建一行Row对象 这里从第二行开始所以+1
            Row row = sheet.createRow(i+1);//这里是空的，需要填充数据
            //填充单元格
            for (int j = 0; j <titles.length ; j++) {
                Cell cell = row.createCell(j);
                //获取用户id的值
                Map<String , Object> rowValue = maps.get(i);
                //循环动态设置多个字段的值
                Object o = rowValue.get(colums[j]);
                //这里也就是为什么查询数据库使用map封装的原因
                cell.setCellValue(o +"");
            }

        }

        return workbook;
    }

    @Override
    public SysUser findByUsername(String username) {
        return sysUserMapper.findByUsername(username);
    }
}
